<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ page import="com.onlineauction.helpers.Constants" %>
<%@ page import="com.onlineauction.helpers.Constants.Category" %>
<%@ page import="com.onlineauction.entities.Auction" %>
<%@ page import="com.onlineauction.entities.Bid" %>
<%@ page import="com.onlineauction.entities.Item" %>
<%@ page import="java.util.List" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.Collections" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<%
	// redirect to Home if session is not set (user is not logged in yet)
	String username = (String) session.getAttribute(Constants.USERNAME);
	if (username == null) {
		request.getRequestDispatcher("/Home").forward(request, response);
	}
	
	Auction auction = null;
	List<Bid> bids = null;
	long itemID = 0;
	String itemName="";
	try
	{
		auction = (Auction) request.getAttribute(Constants.AUCTION);
		if (auction == null) 
	    {
	    	response.sendRedirect("/home.jsp");
	    	return;
	    }
		else
		{
			bids = Bid.getBidsOfAuction(auction.getAuctionID());
			Collections.sort(bids, Collections.reverseOrder());
		}
		
		Item item = new Item(auction.getItemID());
		itemName = item.getProductName();
		
	}
	catch(Exception E)
	{	}
    
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Manage Auctions</title>

	<link rel="stylesheet" href="style.css" />
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
	<script type="text/javascript">// <![CDATA[
		$(document).ready(function() {
		$.ajaxSetup({ cache: false }); // This part addresses an IE bug.  without it, IE will only load the first number and will never refresh
		setInterval(function() {
		$('#RefreshableAuctionContent').load('/ViewAuctionHelper?<%=Constants.AUCTION_ID%>=<%=auction.getAuctionID()%>');
		}, 3000); // the "3000" here refers to the time to refresh the div.  it is in milliseconds. 
		});
	</script>
</head>
<jsp:useBean id="auctionBean" class="com.onlineauction.beans.AuctionBean" scope="page" />

<body>

	<div class="wrapper">
		<div id="Content">
			<div id="Header">
				<H2>View Auction for : <%=itemName %></H2>
				<H6>Note: This page automatically updates itself to reveal the latest bids.</H6>
				<a href="/home.jsp">Go to home page </a>
			</div>
			<hr />
			<div id="RefreshableAuctionContent">
				
				<%
					if(bids.size() > 0)
					{
				%>
				<table>
					<tr>
						<th>Bid placed by</th>
						<th>Bid Price</th>
						<th>Time of the bid</th>
					</tr>
				<%
						for(Bid bid : bids)
						{
				%>
					<tr>
						<td><%=bid.getUsername() %></td>
						<td><%=bid.getBidPrice() %></td>
						<td><%=bid.getBidTime() %></td>
					</tr>				
				<%
						}
				%>
				</table>
				<% 
					}else
					{
				%>
				<h6> No bids have been placed on this auction</h6>
				<%
					}
				%>
				<br>
			</div>
		<!-- end Content -->
		</div>
	</div>
	<!-- end wrapper -->


</body>
</html>
